package com.yong.sbd.common.beans.page;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 * 分页数据
 *
 * @author Kang Yong
 * @date 2025/10/28
 * @since 1.0.0
 */
@Data
public class PageResult<T> {

    /**
     * 当前页码
     */
    private Integer pageNum;
    /**
     * 每页数量
     */
    private Integer pageSize;
    /**
     * 总页数
     */
    private Integer pageTotal;
    /**
     * 总数量
     */
    private Long total;

    /**
     * 数据列表
     */
    private List<T> list;

    /**
     * 带统计信息的map对象
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Map<String, Object> countMap;

    public static <T> PageResult<T> page(List<T> list) {
        PageResult<T> result = new PageResult<>();
        PageInfo<T> pageInfo = new PageInfo<>(list);
        result.setList(list);
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setPageTotal(pageInfo.getPages());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    public static <T> PageResult<T> page(PageInfo<T> pageInfo) {
        PageResult<T> result = new PageResult<>();
        result.setList(pageInfo.getList());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setPageTotal(pageInfo.getPages());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    public static <T> PageResult<T> page(List<T> list, Map<String, Object> countMap) {
        PageResult<T> result = new PageResult<>();
        PageInfo<T> pageInfo = new PageInfo<>(list);
        result.setList(list);
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setPageTotal(pageInfo.getPages());
        result.setTotal(pageInfo.getTotal());
        result.setCountMap(countMap);
        return result;
    }

    public static <T> PageResult<T> page(Integer pageNum, Integer pageSize, Long total, List<T> list) {
        PageResult<T> result = new PageResult<>();
        result.setList(list);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setTotal(total);
        return result;
    }

    public static <T> PageResult<T> page(PageParam pageParam, Long total, List<T> list) {
        PageResult<T> result = new PageResult<>();
        result.setList(list);
        result.setPageNum(pageParam.getPageNum());
        result.setPageSize(pageParam.getPageSize());
        result.setTotal(total);
        return result;
    }

}
